<?php
declare (strict_types = 1);

namespace app\validate;

use think\Validate;

class User extends Validate
{
    /**
     * 定义验证规则
     * 格式：'字段名' =>  ['规则1','规则2'...]
     *
     * @var array
     */
    protected $rule = [
        'name'=>"require|max:2|checkName:cs",
        //'name|用户名'=>"require|max:2|checkName:cs",
        "price"=>"number|between:1,100",
        "email"=>"email",
        "id"=>"number"
    ];



    /**
     * 定义错误信息
     * 格式：'字段名.规则名' =>  '错误信息'
     *
     * @var array
     */
    protected $message = [
        "name.require"=>"姓名要写",
        "name.max"=>"姓名格式错误",
        "price.required"=>"价格要有",
        "price.between"=>"价格不对",
        "email.email"=>"邮箱格式不对",
        "id.number"=>"必须是数值"
    ];

    //验证场景
    protected $scene = [
        "insert"=>["name","price","email"],
        //"edit"=>["name","price"]  //只验证规定的字段
        "route"=>["id"]  //路由验证
    ];
    protected function sceneEdit(){  //只验证规定的字段


        $edit = $this->only(["name","price"])->remove("name","require");
        return $edit;
    }


    public function checkName($value, $rule, $data, $field,$title)
    {
//        print_r($field);
//        echo "<br/>";
//        print_r($value);
//        echo "<br/>";
//        print_r($rule);
//        echo "<br/>";
//        print_r($data);
//        echo "<br/>";
//        print_r($title);
        return $value==$rule?"不允许写这个":true;

    }

//    public function checkName(string $field, $value, $rules, $data, string $title = '', array $msg = [])
//    {
//        print_r($field);
//        echo "<br/>";
//        print_r($value);
//        echo "<br/>";
//        print_r($rules);
//        echo "<br/>";
//        print_r($data);
//        echo "<br/>";
//        print_r($title);
//        echo "<br/>";
//        print_r($msg);
//    }
}
